package com.rybbaby.tss.front.tssRightStatistic.service.imp;

import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssPayment.entity.ViewPaymentDetails;
import com.rybbaby.tss.front.tssRightStatistic.dao.ITssRightStatisticDao;
import com.rybbaby.tss.front.tssRightStatistic.entity.TssRightStatistic;
import com.rybbaby.tss.front.tssRightStatistic.service.ITssRightStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @description 权责收入数据库操作服务实现
 * 
 * @author 雷春琼
 * 
 * @createDate 2017-08-10 上午12:23:36
 */
@Service
@Transactional
public class TssRightStatisticServiceImp extends BaseServiceImp<TssRightStatistic> implements ITssRightStatisticService {

    @Autowired
    private ITssRightStatisticDao tssRightStatisticDao;


    @Override
    public TssRightStatistic findByAttendanceId(Object id) {
        return tssRightStatisticDao.findUniqueByProperty(TssRightStatistic.class,"attendanceId",id);
    }

    @Override
    public boolean saveOrUpdateTssRightStatisticService(TssRightStatistic tssRightStatistic, Map<String, Object> parameterMap,String adminUserId,Integer type,Integer responsibilityType,Integer isDelete,Double amount) {
        boolean flag = false;
        Double amountDouble = 0.0;
        if(tssRightStatistic != null){
            if(isDelete == 1){
                tssRightStatistic.setIsDelete(isDelete);
            }
            tssRightStatistic.setAdminUserId(adminUserId);
            tssRightStatistic.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            tssRightStatisticDao.saveOrUpdate(tssRightStatistic);
            flag = true;
        }else{
            TssRightStatistic rightStatistic = new TssRightStatistic();
            //权责金额
            if(amount != null){
                amountDouble = new BigDecimal(amount).setScale(2, BigDecimal.ROUND_UP).doubleValue();
            }
            //1 4 课程考勤
            if(responsibilityType == 1 || responsibilityType == 4){
                //计算课时权责
                BigDecimal nowUnitPrice = new BigDecimal(parameterMap.get("nowUnitPrice").toString()).setScale(2, BigDecimal.ROUND_UP);
                if(parameterMap.get("formalClass")!=null){
                    BigDecimal formalClass = new BigDecimal(parameterMap.get("formalClass").toString()).setScale(2, BigDecimal.ROUND_UP);
                    amountDouble = nowUnitPrice.multiply(formalClass).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                }else{
                    amountDouble = 0.0;
                }
                BigDecimal expendClassHour = new BigDecimal(parameterMap.get("expendClassHour").toString()).setScale(2, BigDecimal.ROUND_UP);
                Double classAmount = nowUnitPrice.multiply(expendClassHour).setScale(2, BigDecimal.ROUND_UP).doubleValue();

                rightStatistic.setClassAmount(classAmount);
                rightStatistic.setAttendanceId(parameterMap.get("id").toString());
            }
            //2 5考勤退课 负的  权责 negate（）
            if(responsibilityType == 2 || responsibilityType == 5){
                //计算考勤退课  负的  权责 negate（）
                BigDecimal nowUnitPrice = new BigDecimal(parameterMap.get("nowUnitPrice").toString()).setScale(2, BigDecimal.ROUND_UP);
                if(parameterMap.get("formalClass")!=null){
                    BigDecimal formalClass = new BigDecimal(parameterMap.get("formalClass").toString()).setScale(2, BigDecimal.ROUND_UP);
                    amountDouble = nowUnitPrice.multiply(formalClass).setScale(2, BigDecimal.ROUND_UP).negate().doubleValue();
                }else{
                    amountDouble = 0.0;
                }
                rightStatistic.setAttendanceId(parameterMap.get("id").toString());
            }
            //7 考勤扣餐点费
            if(responsibilityType == 7){
                rightStatistic.setAttendanceId(parameterMap.get("id").toString());
            }
            //8 一次性物品缴费
            if(responsibilityType == 8){
                rightStatistic.setPaymentId(parameterMap.get("paymentId").toString());
            }
            //9亲子课时包退费 10过渡课时包退费 11一次性物品费退费 12材料费退费
            if(responsibilityType == 9 || responsibilityType == 10 || responsibilityType == 11 || responsibilityType == 12){
                rightStatistic.setReturnPremiumId(parameterMap.get("returnPremiumId").toString());
            }
            //11一次性物品费退费 负的  权责 negate（）
            if(responsibilityType == 11){
                amountDouble = new BigDecimal(amount).setScale(2, BigDecimal.ROUND_UP).negate().doubleValue();
            }
            //13转历史课时包
            if(responsibilityType == 13){
                String stuType = parameterMap.get("stuType").toString();
                BigDecimal nowUnitPrice = new BigDecimal(parameterMap.get("nowUnitPrice").toString()).setScale(2, BigDecimal.ROUND_UP);
                if("1".equals(stuType)){
                    BigDecimal surplusFormalClassHour = new BigDecimal(parameterMap.get("surplusFormalClassHour").toString()).setScale(2, BigDecimal.ROUND_UP);
                    BigDecimal surplusPresentClassHour = new BigDecimal(parameterMap.get("surplusPresentClassHour").toString()).setScale(2, BigDecimal.ROUND_UP);
                    BigDecimal classHour = surplusFormalClassHour.add(surplusPresentClassHour);
                    amountDouble = nowUnitPrice.multiply(classHour).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                }
                if("2".equals(stuType)){
                    BigDecimal surplusFormalAttendanceDay = new BigDecimal(parameterMap.get("surplusFormalAttendanceDay").toString()).setScale(2, BigDecimal.ROUND_UP);
                    BigDecimal surplusPresentAttendanceDay = new BigDecimal(parameterMap.get("surplusPresentAttendanceDay").toString()).setScale(2, BigDecimal.ROUND_UP);
                    BigDecimal attendanceDay = surplusFormalAttendanceDay.add(surplusPresentAttendanceDay);
                    amountDouble = nowUnitPrice.multiply(attendanceDay).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                }
            }
            //3亲子扣减课时 6过渡扣减课时 14手动扣减课时
            if(responsibilityType == 3 || responsibilityType == 6 || responsibilityType == 14){
                if(parameterMap.get("type").toString() != null){
                    Integer memberPackageType = Integer.parseInt(parameterMap.get("type").toString());
                    BigDecimal nowUnitPrice = new BigDecimal(parameterMap.get("nowUnitPrice").toString()).setScale(2, BigDecimal.ROUND_UP);
                    if(memberPackageType == 1){
                        BigDecimal surplusFormalClassHour = new BigDecimal(parameterMap.get("surplusFormalClassHour").toString()).setScale(2, BigDecimal.ROUND_UP);
                        BigDecimal surplusPresentClassHour = new BigDecimal(parameterMap.get("surplusPresentClassHour").toString()).setScale(2, BigDecimal.ROUND_UP);
                        BigDecimal sumClassHour = surplusFormalClassHour.add(surplusPresentClassHour);
                        amountDouble = nowUnitPrice.multiply(sumClassHour).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                    }
                    if(memberPackageType == 2){
                        BigDecimal surplusFormalAttendanceDay = new BigDecimal(parameterMap.get("surplusFormalAttendanceDay").toString()).setScale(2, BigDecimal.ROUND_UP);
                        BigDecimal surplusPresentAttendanceDay = new BigDecimal(parameterMap.get("surplusPresentAttendanceDay").toString()).setScale(2, BigDecimal.ROUND_UP);
                        BigDecimal sumAttendanceDay = surplusFormalAttendanceDay.add(surplusPresentAttendanceDay);
                        amountDouble = nowUnitPrice.multiply(sumAttendanceDay).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                    }
                    rightStatistic.setMemberPackageId(parameterMap.get("memberPackageId").toString());
                }
            }
            //15考勤退课餐点费  负的  权责 negate（）
            if(responsibilityType == 15){
                rightStatistic.setAttendanceId(parameterMap.get("id").toString());
                amountDouble = new BigDecimal(amount).setScale(2, BigDecimal.ROUND_UP).negate().doubleValue();
            }
            //16转历史餐点费
            if(responsibilityType == 16){
                BigDecimal mealFeeAmount = new BigDecimal(parameterMap.get("amount").toString()).setScale(2, BigDecimal.ROUND_UP);
                BigDecimal consumptionAmount = new BigDecimal(parameterMap.get("consumptionAmount").toString()).setScale(2, BigDecimal.ROUND_UP);
                amountDouble = mealFeeAmount.subtract(consumptionAmount).setScale(2, BigDecimal.ROUND_UP).doubleValue();
            }

            rightStatistic.setStuId(parameterMap.get("memberId").toString());
            rightStatistic.setType(type);
            rightStatistic.setResponsibilityType(responsibilityType);
            rightStatistic.setAmount(amountDouble);
            rightStatistic.setAdminUserId(adminUserId);
            rightStatistic.setIsDelete(isDelete);
            rightStatistic.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            rightStatistic.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            Serializable serializable = tssRightStatisticDao.save(rightStatistic);
            if(serializable!=null){
                flag = true;
            }
        }
        return flag;
    }


    @Override
    public TssRightStatistic findByAttendanceIdAndtype(Object attendanceId, Object type) throws Exception{
        List<TssRightStatistic> byStuIdAndtype = tssRightStatisticDao.findByAttendanceIdAndtype(attendanceId,type);
        if(byStuIdAndtype.size() >0){
            return byStuIdAndtype.get(0);
        }
        return null;
    }

    @Override
    public TssRightStatistic findByReturnPremiumId(String returnPremiumId) throws Exception{
        List<TssRightStatistic> byStuIdAndtype = tssRightStatisticDao.findByReturnPremiumId(returnPremiumId);
        if(byStuIdAndtype.size() >0){
            return byStuIdAndtype.get(0);
        }
        return null;
    }

    @Override
    public Map<String, Object> findTodayResponsibility(String adminUserIds) {
        return tssRightStatisticDao.findTodayResponsibility(adminUserIds);
    }

    @Override
    public Map<String, Object> findMonthResponsibility(String adminUserIds) {
        return tssRightStatisticDao.findMonthResponsibility(adminUserIds);
    }
}
